package com.exampleyyy.demoyyy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exampleyyy.demoyyy.ServiceJieKou.SetmealServiceJieKou;
import com.exampleyyy.demoyyy.ShiTi.DishFlavor;
import com.exampleyyy.demoyyy.ShiTiJieKou.SetmealJieKou;
import com.exampleyyy.demoyyy.ShiTi.Setmeal;
import com.exampleyyy.demoyyy.ShiTi.SetmealDish;
import com.exampleyyy.demoyyy.dto.SetmealDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealService extends ServiceImpl<SetmealJieKou, Setmeal> implements SetmealServiceJieKou {
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Transactional
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            throw new RuntimeException("套餐正在出售中，不能删除");
        }
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        // 删除关系表中的数据
        setmealDishService.remove(queryWrapper1);;
    }

    public SetmealDto getByIdWithFlavor(Long id) {
        //查询套餐基本信息，从dish表查询
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> flavors = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(flavors);
        return setmealDto;
    }

    /**
     * 修改套餐
     */
    @Transactional
    public void updateWithFlavor(SetmealDto setmealDto) {
        //更新dish表基本信息
        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(queryWrapper);
        List<SetmealDish> flavors = setmealDto.getSetmealDishes();
        flavors = flavors.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(flavors);
    }
}
